package com.fattyCorps.game.controller;

import com.fattyCorps.game.obj.msg.SRet;
import com.fattyCorps.game.obj.msg.client.item.CAddItemMsg;
import com.fattyCorps.game.obj.msg.client.item.CSaleItemMsg;
import com.fattyCorps.game.obj.msg.client.item.CUseItemMsg;
import com.fattyCorps.game.service.ItemService;
import com.fattyCorps.game.util.GameJwtUtils;
import com.fattyCorps.game.an.AMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    ItemService itemService;

    @PostMapping("/addItem")
    public SRet addItem(HttpServletRequest request, @RequestBody CAddItemMsg cAddItemMsg){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        itemService.addItem(playerId, cAddItemMsg.getItemTypeId(), cAddItemMsg.getNum());
        return SRet.success();
    }

    // 获取所有道具信息
    @PostMapping("/getItemList")
    @AMsgType(readOnly = true)
    public SRet getItemList(HttpServletRequest request){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
//        通过itemService.getItemList获取列表
        SRet itemList = itemService.getItemList(playerId);
        return itemList;
    }

    // 使用道具
    @PostMapping("/useItem")
    public SRet useItem(HttpServletRequest request, @RequestBody CUseItemMsg cUseItemMsg){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        SRet sRet = itemService.useItem(playerId, cUseItemMsg.getItemId(), cUseItemMsg.getNum());
        return sRet;
    }

    @PostMapping("/saleItem")
    public SRet saleItem(HttpServletRequest request, @RequestBody CSaleItemMsg cUseItemMsg){
        Integer playerId = GameJwtUtils.getPlayerIdByRequest(request);
        SRet sRet = itemService.saleItem(playerId, cUseItemMsg.getItemId(), cUseItemMsg.getNum());
        return sRet;
    }
}
